package com.gcd.gym.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.gym.model.Admin;
import com.gcd.gym.service.AdminService;
import com.gcd.gym.utils.JsonResult;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;

@RestController
@RequestMapping(value = "/api/v1/admin", produces = MediaType.APPLICATION_JSON_VALUE)
public class AdminApi {


    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }


    //查询管理员
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAdmin(@RequestParam(defaultValue = "1") @Validated Integer pageNo,
                                                   @RequestParam(defaultValue = "10") @Validated Integer pageSize,
                                                   Admin admin) {

        Page<Admin> page = new Page<>(pageNo, pageSize);
        page = adminService.findAllAdmin(page, admin);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    //删除管理员
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteAdmin(@RequestBody @Validated Integer[] ids) {
        int count = adminService.deleteById(Arrays.asList(ids));
        if (count == 0) {
            return ResponseEntity.ok(JsonResult.fail("删除失败，后台异常"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }


    //添加管理员
    @PostMapping
    public ResponseEntity<JsonResult<?>> addAdmin(@RequestBody @Validated Admin admin) {
        boolean success = adminService.addAdmin(admin);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("添加管理员失败"));
        }
    }

    //修改管理员
    @PutMapping
    public ResponseEntity<JsonResult<?>> editAdmin(@RequestBody @Validated Admin admin) {
        boolean success = adminService.editAdmin(admin);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("添加管理员失败"));
        }


        //todo 验证密码
        //todo 数据校验
        //todo 课程日历视图
        //todo 文件导出
    }
}
